package com.isunland.managesystem.ui;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.NavUtils;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.isunland.managesystem.R;
import com.isunland.managesystem.base.BaseVolleyActivity;
import com.isunland.managesystem.entity.ShouldGathPayList.GatheringDetail;

/**
 * 应收付款查询详情
 *
 * @author wu
 */
public class ShouldGatherPayDetailFragment extends Fragment {
  public static final String EXTRA_PAY_VALUE =
      "com.isunland.managesystem.ui.ShouldGatherPayDetailFragment.EXTRA_PAY_VALUE";
  public static final String EXTRA_TYPE =
      "com.isunland.managesystem.ui.ShouldGatherPayDetailFragment.EXTRA_TYPE";
  public static final String EXTRA_PAY =
      "com.isunland.managesystem.ui.ShouldGatherPayDetailFragment.EXTRA_PAY";
  public static final String EXTRA_GATHER_VALUE =
      "com.isunland.managesystem.ui.ShouldGatherPayDetailFragment.EXTRA_GATHER_VALUE";
  public static final String EXTRA_GATHER =
      "com.isunland.managesystem.ui.ShouldGatherPayDetailFragment.EXTRA_GATHER";
  private TextView mtvContractDeliveryGetherPayDetail;// 合同订单时间
  private String mTime;
  private TextView mtvCustomerGetherPayDetail;// 客户单位
  private String mUnit;
  private TextView mtvTypeGetherPayDetail;// 类型
  private String mType;
  private TextView tvContractDeliveryNameGetherPayDetail;// 订单合同名称
  private String mOrderName;
  private TextView mtvContractDeliveryNumGetherPayDetail;// 订单合同编号
  private String mOrderNum;
  private TextView mtvHappenedCostGetherPayDetail;// 发生金额
  private String mCost;

  private LinearLayout mllGatherDetail;// 应收账款
  private TextView mtvHasCalculateDetailGetherPayDetail;// 已结算
  private String mHasCalculate;
  private TextView mtvNotCalculateDetailGetherPayDetail;// 未结算
  private String mNotCalculate;
  private TextView mtvHasOpenBillingGetherPayDetail;// 已开票
  private String mHasOpenBill;
  private TextView mtvNoOpenBillingGetherPayDetail;// 未开票
  private String mNotOpenBill;

  private LinearLayout mllPayDetail;// 应付账款
  private TextView mtvHasPayDetailGetherPayDetail;// 已支付
  private String mHasPay;
  private TextView mtvNotPayDetailGetherPayDetail;// 未支付
  private String mNotPay;
  private TextView mtvHasacceptBillingGetherPayDetail;// 已收票
  private String mAccetped;
  private TextView mtvNoacceptBillingGetherPayDetail;// 未收票
  private String mNotAccepted;

  GatheringDetail mPayDetail;
  GatheringDetail mGhtherDetail;
  private String mGatherPayType;

  @Override public void onCreate(Bundle savedInstanceState) {
    // 设置返回图标
    setHasOptionsMenu(true);
    if (NavUtils.getParentActivityName(getActivity()) != null) {
      ((BaseVolleyActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }
    mPayDetail = (GatheringDetail) getArguments().getSerializable(EXTRA_PAY_VALUE);
    mGhtherDetail = (GatheringDetail) getArguments().getSerializable(EXTRA_GATHER_VALUE);
    mGatherPayType = getArguments().getString(EXTRA_TYPE);
    if (EXTRA_GATHER.equalsIgnoreCase(mGatherPayType)) {
      ((BaseVolleyActivity) getActivity()).getSupportActionBar()
          .setTitle(R.string.shouldGatherDtail);
    } else if (EXTRA_PAY.equalsIgnoreCase(mGatherPayType)) {
      ((BaseVolleyActivity) getActivity()).getSupportActionBar().setTitle(R.string.shouldPayDtail);
    }
    if (mPayDetail != null) {
      mTime = mPayDetail.getRegDate();
      mUnit = mPayDetail.getPartName();
      mType = mPayDetail.getContractKindName();
      mOrderName = mPayDetail.getContractName();
      mOrderNum = mPayDetail.getContractCode();
      mCost = mPayDetail.getContractAmount();
      mHasPay = mPayDetail.getMakedContractAmount();
      mNotPay = mPayDetail.getResidualContractAmount();
      mAccetped = mPayDetail.getMakedInvoiceAmount();
      mNotAccepted = mPayDetail.getResidualInvoiceAmount();
    }

    if (mGhtherDetail != null) {
      mTime = mGhtherDetail.getRegDate();
      mUnit = mGhtherDetail.getPartName();
      mType = mGhtherDetail.getContractKindName();
      mOrderName = mGhtherDetail.getContractName();
      mOrderNum = mGhtherDetail.getContractCode();
      mCost = mGhtherDetail.getContractAmount();
      mHasCalculate = mGhtherDetail.getMakedContractAmount();
      mNotCalculate = mGhtherDetail.getResidualContractAmount();
      mHasOpenBill = mGhtherDetail.getMakedInvoiceAmount();
      mNotOpenBill = mGhtherDetail.getResidualInvoiceAmount();
    }
    super.onCreate(savedInstanceState);
  }

  @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_gather_pay_detail, container, false);
    mtvContractDeliveryGetherPayDetail =
        (TextView) view.findViewById(R.id.tv_contractDelivery_getherPayDetail);
    mtvCustomerGetherPayDetail = (TextView) view.findViewById(R.id.tv_customer_getherPayDetail);
    mtvTypeGetherPayDetail = (TextView) view.findViewById(R.id.tv_type_getherPayDetail);
    tvContractDeliveryNameGetherPayDetail =
        (TextView) view.findViewById(R.id.tv_contractDeliveryName_getherPayDetail);
    mtvContractDeliveryNumGetherPayDetail =
        (TextView) view.findViewById(R.id.tv_contractDeliveryNum_getherPayDetail);
    mtvHappenedCostGetherPayDetail =
        (TextView) view.findViewById(R.id.tv_happenedCost_getherPayDetail);

    mllGatherDetail = (LinearLayout) view.findViewById(R.id.ll_gather_detail);
    mtvHasCalculateDetailGetherPayDetail =
        (TextView) view.findViewById(R.id.tv_hasCalculateDetail_getherPayDetail);
    mtvNotCalculateDetailGetherPayDetail =
        (TextView) view.findViewById(R.id.tv_notCalculateDetail_getherPayDetail);
    mtvHasOpenBillingGetherPayDetail =
        (TextView) view.findViewById(R.id.tv_hasOpenBilling_getherPayDetail);
    mtvNoOpenBillingGetherPayDetail =
        (TextView) view.findViewById(R.id.tv_noOpenBilling_getherPayDetail);

    mllPayDetail = (LinearLayout) view.findViewById(R.id.ll_pay_detail);
    mtvHasPayDetailGetherPayDetail =
        (TextView) view.findViewById(R.id.tv_hasPayDetail_getherPayDetail);
    mtvNotPayDetailGetherPayDetail =
        (TextView) view.findViewById(R.id.tv_notPayDetail_getherPayDetail);
    mtvHasacceptBillingGetherPayDetail =
        (TextView) view.findViewById(R.id.tv_hasacceptBilling_getherPayDetail);
    mtvNoacceptBillingGetherPayDetail =
        (TextView) view.findViewById(R.id.tv_noacceptBilling_getherPayDetail);
    initData();
    return view;
  }

  private void initData() {

    if (!TextUtils.isEmpty(mTime)) {
      mtvContractDeliveryGetherPayDetail.setText(mTime);
    }
    if (!TextUtils.isEmpty(mUnit)) {
      mtvCustomerGetherPayDetail.setText(mUnit);
    }
    if (!TextUtils.isEmpty(mType)) {
      mtvTypeGetherPayDetail.setText(mType);
    }
    if (!TextUtils.isEmpty(mOrderName)) {
      tvContractDeliveryNameGetherPayDetail.setText(mOrderName);
    }
    if (!TextUtils.isEmpty(mOrderNum)) {
      mtvContractDeliveryNumGetherPayDetail.setText(mOrderNum);
    }
    if (!TextUtils.isEmpty(mCost)) {
      mtvHappenedCostGetherPayDetail.setText(mCost);
    }

    if (EXTRA_GATHER.equalsIgnoreCase(mGatherPayType)) {
      mllPayDetail.setVisibility(View.GONE);
      if (!TextUtils.isEmpty(mHasCalculate)) {
        mtvHasCalculateDetailGetherPayDetail.setText(mHasCalculate);
      }
      if (!TextUtils.isEmpty(mNotCalculate)) {
        mtvNotCalculateDetailGetherPayDetail.setText(mNotCalculate);
      }
      if (!TextUtils.isEmpty(mHasOpenBill)) {
        mtvHasOpenBillingGetherPayDetail.setText(mHasOpenBill);
      }
      if (!TextUtils.isEmpty(mNotOpenBill)) {
        mtvNoOpenBillingGetherPayDetail.setText(mNotOpenBill);
      }
    }

    if (EXTRA_PAY.equalsIgnoreCase(mGatherPayType)) {
      mllGatherDetail.setVisibility(View.GONE);
      if (!TextUtils.isEmpty(mHasPay)) {
        mtvHasPayDetailGetherPayDetail.setText(mHasPay);
      }
      if (!TextUtils.isEmpty(mNotPay)) {
        mtvNotPayDetailGetherPayDetail.setText(mNotPay);
      }
      if (!TextUtils.isEmpty(mAccetped)) {
        mtvHasacceptBillingGetherPayDetail.setText(mAccetped);
      }
      if (!TextUtils.isEmpty(mNotAccepted)) {
        mtvNoacceptBillingGetherPayDetail.setText(mNotAccepted);
      }
    }
  }

  public static Fragment newInstance(GatheringDetail payDetail, GatheringDetail gatherDetail,
      String type) {
    Bundle bundle = new Bundle();
    bundle.putSerializable(EXTRA_PAY_VALUE, payDetail);
    bundle.putSerializable(EXTRA_GATHER_VALUE, gatherDetail);
    bundle.putString(EXTRA_TYPE, type);
    ShouldGatherPayDetailFragment fragment = new ShouldGatherPayDetailFragment();
    fragment.setArguments(bundle);
    return fragment;
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      // 响应返回图标
      case android.R.id.home:

        if (NavUtils.getParentActivityName(getActivity()) != null) {
          NavUtils.navigateUpFromSameTask(getActivity());
        }
      default:
        break;
    }
    return super.onOptionsItemSelected(item);
  }
}
